package com.qen.zhihulanzhou

import cn.hutool.core.lang.Console
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.support.ui.ExpectedConditions
import org.openqa.selenium.support.ui.WebDriverWait
import java.time.Duration

fun main() {
    System.setProperty("webdriver.chrome.driver", "/Users/a1/Documents/Git/Kotlin/Eleven/chrome/chromedriver")

    println("进来了")
    val driver: WebDriver = ChromeDriver()
    try{
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30))
        driver.get("https://www.lanzoux.com/iA47bqyj67c")
//    driver.get("https://www.lanzoux.com/fn?VTNUPlw1D2wBZwBmAGxSZFo3ADdXLlAmUGpRZlU_bBjQDM1o_aDWUAYgFkUTFWMVZxV3oDYwI_aVicKZFc2VmRSOFUwVHpcPA8ZATYAAgAt")
        val title = driver.title
        println(title)

        val wait = WebDriverWait(driver, Duration.ofSeconds(30))

        val iframe = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("ifr2")))
        driver.switchTo().frame(iframe)
        Console.log(iframe)
//        val tourl = iframe.findElement(By.id("tourl"))
//        Console.log(tourl)
        val element: WebElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("tourl")))
        Console.log(element)
        if (element == null) {
            println("没有发现元素")
        } else {
            val a = element.findElement(By.tagName("a"))
            val href = a.getAttribute("href")
            Console.log(href)
            val elementList = element.findElements(By.className("txt"))
            val elementBtn = elementList.get(2)
            elementBtn.click()
            println("发现元素")
        }
//    val element: WebElement = driver.findElement(By.className("load"))

        println(element)
    }finally {
        driver.quit()
    }
    println("结束了")


}